package com.sunnada.exam.vo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sunnada.exam.enmu.ResultType;
import com.sunnada.exam.po.ResultsPo;
import lombok.Data;

@Data
public class ResultsVo {
    private Integer id;
    private String name;
    private Integer tid;
    private String resultType = ResultType.dominance.getName();
    private Integer highestScore;
    private Integer dominanceScore;
    private Integer extroversionScore;
    private Integer patienceScore;
    private Integer precisionScore;
    private Integer conformityScore;

    public ResultsVo() {
    }

    public ResultsVo(ResultsPo resultsPo) {
        this.id = resultsPo.getId();
        this.name = resultsPo.getName();
        this.tid = resultsPo.getTid();
        this.highestScore = 0;
        JSONArray jsonArray = JSON.parseArray(resultsPo.getResult());
        for (Object o : jsonArray) {
            JSONObject json = (JSONObject)o;
            String resultType = json.getString("resultType");
            Integer result = json.getInteger("result");
            saveScoreAndHighest(resultType,result);
        }
    }

    private void saveScoreAndHighest(String resultType,Integer result){
        switch (resultType){
            case "dominance":{
                this.dominanceScore = result;
                break;
            }
            case "extroversion":{
                this.extroversionScore = result;
                break;
            }
            case "patience":{
                this.patienceScore = result;
                break;
            }
            case "precision":{
                this.precisionScore = result;
                break;
            }
            case "conformity":{
                this.conformityScore = result;
                break;
            }
        }
        if (result>this.highestScore){
            this.highestScore = result;
            if (ResultType.dominance.getPage().equals(resultType))this.resultType = ResultType.dominance.getName();
            if (ResultType.extroversion.getPage().equals(resultType))this.resultType = ResultType.extroversion.getName();
            if (ResultType.patience.getPage().equals(resultType))this.resultType = ResultType.patience.getName();
            if (ResultType.precision.getPage().equals(resultType))this.resultType = ResultType.precision.getName();
            if (ResultType.conformity.getPage().equals(resultType))this.resultType = ResultType.conformity.getName();
        }
    }
}
